﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :D_T_StockExchange_Exp
//        description :股票价格操作相关
//
//        created by 何庆 at  2011年1月3日
//
//======================================================================
namespace huaguwang
{
      /// <summary>
      /// 股票价格操作相关
      /// </summary>
      public class D_T_StockExchange_Exp : D_T_StockExchange
      {
            public class StockExchage : M_T_StockExchange
            {
                  public string Code { get; set; }
                  public string Title { get; set; }
            }

            public List<StockExchage> GetStockExchageList(int begin, int pageSize, string stockName, string stockCode, DateTime? dtBegin, DateTime? dtEnd, ref int queryCount)
            {
                  string strDateTimeFilter = string.Empty;
                  if (dtBegin == null && dtEnd == null)
                  {
                        strDateTimeFilter = " and a.exchdate between '' and getdate() ";
                  }
                  else if (dtBegin == null && dtEnd != null)
                  {
                        strDateTimeFilter = " and a.exchdate between '' and '" + dtEnd.Value.ToString() + "'";
                  }
                  else if (dtBegin != null && dtEnd == null)
                  {
                        strDateTimeFilter = " and a.exchdate between '" + dtBegin.Value.ToString() + "' and getdate() ";
                  }
                  else
                  {
                        strDateTimeFilter = " and a.exchdate between '" + dtBegin.Value.ToString() + "' and '" + dtEnd.Value.ToString()+"'";
                  }
                  string strTable = " T_StockExchange as a inner join T_StockCode as b on a.realcode = b.id ";
                  string strWhere = " a.flag = 1 and b.flag = 1 and b.code like '%'+@stockcode+'%' and b.title like '%'+@stocktitle+'%'  ";
                  strWhere = strWhere + strDateTimeFilter;
                  string strQuerycountSQL = ExpandMSSQL.GetPageSQL(strTable, strWhere);
                  SqlParameter[] sqlParam = 
                  {
                        new SqlParameter("@stockcode",SqlDbType.NVarChar,50){ Value=stockCode},
                        new SqlParameter("@stocktitle", SqlDbType.NVarChar,1000){ Value=stockName}
                  };
                  queryCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strQuerycountSQL, sqlParam).Exp_IntTryParse();

                  string strSQL = ExpandMSSQL.GetPageSQL(begin, pageSize, "a.*,b.Code,b.Title", strTable, "a.id", strWhere, "a.ExchDate desc,b.Code");
                  var vResult = MSSQLHelper.SQLConType.C1.Exp_GetList<StockExchage>(strSQL, row => new StockExchage
                  {
                        id = row["id"].Exp_IntTryParse(),
                        Code = row["Code"].Exp_Trim(),
                        ExchDate = row["ExchDate"].Exp_DateTimeTryParse(),
                        Flag = true,
                        Price = row["Price"].Exp_DecimalTryParse(),
                        RealCode = row["RealCode"].Exp_Trim(),
                        StockCode = row["StockCode"].Exp_Trim(),
                        Title = row["Title"].Exp_Trim(),
                        YesPrice = row["YesPrice"].Exp_DecimalTryParse()
                  }, sqlParam);
                  return vResult;
            }
      }
}
